﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;

namespace HdipCricketClub.Models
{
    public class OpponentFixturePlayerRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public IQueryable<OpponentFixturePlayer> GetOpponentPlayers(int id)
        {
            return db.OpponentFixturePlayers.Include("Fixture").Include("DismissalType").Include("Member").Include("Member1")
                                            .Where(ofp => ofp.op_fixture_id == id)
                                            .OrderBy(ofp => ofp.op_bowler_number);
        }

        public IQueryable<OpponentFixturePlayer> GetOpponentBatsmen(int id)
        {
            return db.OpponentFixturePlayers.Include("Fixture").Include("DismissalType").Include("Member").Include("Member1")
                                            .Where(ofp => ofp.op_fixture_id == id && ofp.op_batsman_number != null)
                                            .OrderBy(ofp => ofp.op_batsman_number);
        }

        public IQueryable<OpponentFixturePlayer> GetOpponentBowlers(int id)
        {
            return db.OpponentFixturePlayers.Include("Fixture").Include("DismissalType").Include("Member").Include("Member1")
                                            .Where(ofp => ofp.op_fixture_id == id && ofp.op_bowler_number != null)
                                            .OrderBy(ofp => ofp.op_bowler_number);
        }

        public double TotalOpponentBatsmensRuns(int id)
        {
            var opponentFixtureBatsmen = db.OpponentFixturePlayers.Where(ofp => ofp.Fixture.fixture_id == id);

            double totalOpponentBatsmenRuns = 0;

            foreach (var batsman in opponentFixtureBatsmen)
            {
                if (batsman.op_batting_runs != null)
                {
                    totalOpponentBatsmenRuns += (double)batsman.op_batting_runs;
                }
            }
            return totalOpponentBatsmenRuns;
        }

    }
}